package dashboard.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bj58.argo.ActionResult;
import com.bj58.argo.annotations.GET;
import com.bj58.argo.annotations.POST;
import com.bj58.argo.annotations.Path;
import com.bj58.argo.controller.AbstractController;
import com.bj58.ecdata.calc.dbservice.service.BusslineEnum;
import com.bj58.ecdata.calc.dbservice.service.MapTopTypeEnum;
import com.bj58.ecdata.calc.dbservice.service.MarketTypeEnum;
import com.bj58.ecdata.calc.dbservice.service.PlatformEnum;

import dashboard.constant.EnumCate;
import dashboard.databll.ClickServiceBLL;
import dashboard.databll.DisplayServBLL;
import dashboard.service.SCFClient;
import dashboard.utils.Option;
import dashboard.utils.VelocityTools;
import net.sf.json.JSONObject;

@Path("/realtime")
public class RealTimeController extends AbstractController {

	private static final Log log = LogFactory.getLog(RealTimeController.class);
	private static final List<Option> busslineList = new ArrayList<Option>();
	private static final List<Option> platList = new ArrayList<Option>();
	private static final VelocityTools vt = new VelocityTools();

	{
		SCFClient.initialSCF();
		// System.out.println(DataDispService.ping());
		// 业务线选择
		busslineList.add(new Option("房产", "fangchan"));
		busslineList.add(new Option("黄页", "huangye"));
		busslineList.add(new Option("招聘", "zhaopin"));
		busslineList.add(new Option("二手", "ershou"));
		busslineList.add(new Option("二手车", "ershouche"));
		// 平台选择
		platList.add(new Option("PC", "PC"));
		platList.add(new Option("M", "M"));
		platList.add(new Option("APP", "APP"));

	}

	@POST
	@Path("/cpc/getCashCost")
	public ActionResult getCashCost() {
		String bussline_s = beat().getRequest().getParameter("bussline");
		String platform_s = beat().getRequest().getParameter("platform");

		BusslineEnum bussline = null;
		PlatformEnum platform = null;
		try {
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
		} catch (Exception e) {
			log.info(e);
		}
		return ViewResult.jsonView(ClickServiceBLL.getDailySum(platform, bussline));
	}

	@GET
	@Path("/cpc")
	public ActionResult CPCRealTimeDashboard() {
		Map<String, Object> context = beat().getModel().getModel();
		try {
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			if ("total".equals(businessOpers[0])) {
				businessOpers[0] = "";
			}
			String bussline_s = beat().getRequest().getParameter("bussline");
			String platform_s = beat().getRequest().getParameter("platform");

			BusslineEnum bussline = null;
			PlatformEnum platform = null;
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);

			context.put("bussline", bussline == null ? "" : bussline.value());
			context.put("platform", platform == null ? "" : platform.name());
			context.put("busslineList", businessOpers);
			context.put("business_CN_Map", EnumCate.business_CN_Map);
			context.put("platList", platList);
			context.put("vt", vt);
			// 消费和现金
			JSONObject cashcost = JSONObject.fromObject(ClickServiceBLL.getDailySum(platform, bussline));
			context.put("cash", cashcost.get("cash"));
			context.put("cost", cashcost.get("cost"));

			return view("realtime/cpc");
		} catch (Exception e) {
			log.info(e);
			return ViewResult.jsonView("");
		}
	}

	@POST
	@Path("/cpc/getHourData")
	public ActionResult getCPCHourData() {
		String type_s = beat().getRequest().getParameter("type");
		String bussline_s = beat().getRequest().getParameter("bussline");
		String platform_s = beat().getRequest().getParameter("platform");
		String time_s = beat().getRequest().getParameter("time");
		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			PlatformEnum platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
			long time = NumberUtils.toLong(time_s, 0);

			String[] type = StringUtils.split(type_s, "_");
			if (StringUtils.equalsIgnoreCase(type[0], "click")) {
				return ViewResult.jsonpView(ClickServiceBLL.getData(type[1], platform, bussline, time));
			} else if (StringUtils.equalsIgnoreCase(type[0], "disp")) {
				return ViewResult.jsonpView(DisplayServBLL.getData(type[1], platform, bussline, time));
			}
		} catch (Exception e) {
		}

		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/cpc/getDayData")
	public ActionResult getCPCDayData() {
		String type_s = beat().getRequest().getParameter("type");
		String bussline_s = beat().getRequest().getParameter("bussline");
		String platform_s = beat().getRequest().getParameter("platform");
		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			PlatformEnum platform = StringUtils.isBlank(platform_s) ? null : PlatformEnum.valueOf(platform_s);
			String[] type = StringUtils.split(type_s, "_");
			if (StringUtils.equalsIgnoreCase(type[0], "click")) {
				JSONObject yes = JSONObject.fromObject(ClickServiceBLL.getDataYesterdayHourly(type[1], platform,
						bussline));
				JSONObject now = JSONObject.fromObject(ClickServiceBLL.getDataTodayHourly(type[1], platform, bussline));
				now.element("yesdata", yes.get("data"));
				return ViewResult.jsonpView(now.toString());
			} else if (StringUtils.equalsIgnoreCase(type[0], "disp")) {
				JSONObject yes = JSONObject.fromObject(DisplayServBLL.getDataYesterdayHourly(type[1], platform,
						bussline));
				JSONObject now = JSONObject.fromObject(DisplayServBLL.getDataTodayHourly(type[1], platform, bussline));
				now.element("yesdata", yes.get("data"));
				return ViewResult.jsonpView(now.toString());
			}

		} catch (Exception e) {
		}
		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/cpc/map/getMapData")
	public ActionResult getMapData() {

		JSONObject o = new JSONObject();
		// JSONObject total_clickpv =
		// JSONObject.fromObject(DataDispService.getMapDataTotalWithToday(TypeEnum.CLICK_PV));
		// JSONObject total_clickcash =
		// JSONObject.fromObject(DataDispService.getMapDataTotalWithToday(TypeEnum.CLICK_CASH));
		//
		// o.element("click_pv", total_clickpv.get("total"));
		// o.element("click_cash", total_clickcash.get("total"));

		;

		JSONObject data_d = JSONObject.fromObject(ClickServiceBLL.getMapDataWithCity(MarketTypeEnum.DIRECT_SALE, 0));
		JSONObject data_c = JSONObject.fromObject(ClickServiceBLL.getMapDataWithCity(MarketTypeEnum.CHANNEL, 0));

		// JSONObject data_d =
		// JSONObject.fromObject(DataDispService.getMapData(MarketTypeEnum.DIRECT_SALE,
		// 0));
		// JSONObject data_c =
		// JSONObject.fromObject(DataDispService.getMapData(MarketTypeEnum.CHANNEL,
		// 0));

		o.element("time", data_d.getJSONArray("data").getJSONObject(0).get("time"));
		o.element("city_direct", data_d.getJSONArray("data").getJSONObject(0).getJSONArray("value"));
		o.element("city_channel", data_c.getJSONArray("data").getJSONObject(0).getJSONArray("value"));

		// System.out.println(o);

		return ViewResult.jsonView(o.toString());

	}

	@POST
	@Path("/cpc/map/getMapDataTotal")
	public ActionResult getMapDataTotal() {

		JSONObject o = new JSONObject();

		JSONObject result = JSONObject.fromObject(ClickServiceBLL.getDailySum());

		o.element("time", result.get("time"));
		o.element("click_pv", result.get("pv"));
		String cash = result.get("cash").toString();
		if (cash != null && cash.length() > 5) {
			int count = cash.length() - 5;
			cash = StringUtils.repeat("*", count) + cash.substring(count);
		}
		o.element("click_cash", cash);

		return ViewResult.jsonView(o.toString());

	}

	@POST
	@Path("/cpc/map/getRanking")
	public ActionResult getRanking() {
		String type_s = beat().getRequest().getParameter("type");

		MapTopTypeEnum type = null;
		try {
			type = StringUtils.isBlank(type_s) ? null : MapTopTypeEnum.valueOf(type_s);

		} catch (Exception e) {
			return ViewResult.jsonView("{}");
		}
		String result = ClickServiceBLL.getMapTopList(type, 10);
		return ViewResult.jsonView(result);

	}

	@POST
	@Path("/cpc/map/getDetail")
	public ActionResult getDetail() {
		String time_s = beat().getRequest().getParameter("time");

		long time = NumberUtils.toLong(time_s, 0);

		return ViewResult.jsonView(ClickServiceBLL.getMapDataDetail(100, time));

	}

	@GET
	@Path("/cpc/cpc_map")
	public ActionResult CPCRealTimeMap() {
		return view("realtime/cpc_map");
	}

	@GET
	@Path("/tv/tv-map")
	public ActionResult tvmap() {
		return view("tv/tv-map");
	}
}
